﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FineSoft.Controls
{
	public class Cover : System.Windows.Controls.Primitives.Popup
	{
		private System.Windows.Controls.Border _Mask;
		protected System.Windows.Controls.Border Mask
		{
			get
			{
				if (this._Mask == null)
				{
					this._Mask = new System.Windows.Controls.Border();
					this.Mask.Visibility = System.Windows.Visibility.Collapsed;
					this._Mask.BorderThickness = new System.Windows.Thickness(2);
					this._Mask.Opacity = 0.2;
					this._Mask.BorderBrush = System.Windows.Media.Brushes.Navy;
					this._Mask.Background = System.Windows.Media.Brushes.Blue;
					this._Mask.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
					this._Mask.VerticalAlignment = System.Windows.VerticalAlignment.Top;
				}
				return this._Mask;
			}
		}

		public void ShowMask(System.Windows.Point position, System.Windows.Size size)
		{
			this.ShowMask(new System.Windows.Rect(position.X, position.Y, size.Width, size.Height));
		}

		public void ShowMask(System.Windows.Point position, int width, int height)
		{
			this.ShowMask(new System.Windows.Rect(position.X, position.Y, width, height));
		}

		public void ShowMask(int left, int top, System.Windows.Size size)
		{
			this.ShowMask(new System.Windows.Rect(left, top, size.Width, size.Height));
		}

		public void ShowMask(int left, int top, int width, int height)
		{
			this.ShowMask(new System.Windows.Rect(left, top, width, height));
		}

		public void ShowMask(System.Windows.Rect position)
		{
			//this.ClearMask();
			this.Mask.Margin = new System.Windows.Thickness(position.Left, position.Top, 0, 0);
			this.Mask.Width = position.Width;
			this.Mask.Height = position.Height;
			this.Mask.Visibility = System.Windows.Visibility.Visible;
		}

		public void ClearMask()
		{
			this.Mask.Visibility = System.Windows.Visibility.Collapsed;
			//this.CurrentPageTab.Children.Remove(this.Mask);
		}

		public Cover()
		{
			//this.IsOpen = true;
			//this.AllowsTransparency = true;
			//this.Placement = System.Windows.Controls.Primitives.PlacementMode.Relative;
			//this.Child = new System.Windows.Controls.StackPanel
			//{
			//    Background = System.Windows.Media.Brushes.White,
			//    Opacity = 0.01
			//};
		}

		protected System.Windows.FrameworkElement PlacementTargetElement
		{
			get
			{
				return (System.Windows.FrameworkElement)this.PlacementTarget;
			}
		}

		protected override void OnInitialized(EventArgs e)
		{
			base.OnInitialized(e);


			System.Windows.Window window = System.Windows.Window.GetWindow(this);
			window.LocationChanged += new EventHandler(Window_LocationChanged);
			window.Activated += new EventHandler(Window_Activated);
			window.Deactivated += new EventHandler(Window_Deactivated);
			window.StateChanged += new EventHandler(Window_StateChanged);
			this.PlacementTargetElement.SizeChanged += new System.Windows.SizeChangedEventHandler(PlacementTarget_SizeChanged);

			this.AllowsTransparency = true;
			this.Placement = System.Windows.Controls.Primitives.PlacementMode.Relative;

			System.Windows.Controls.Grid containerGrid = new System.Windows.Controls.Grid();
			//containerGrid.Children.Add(this.Mask);
			System.Windows.Controls.Grid coverGrid = new System.Windows.Controls.Grid();
			coverGrid.Background = System.Windows.Media.Brushes.White;
			coverGrid.Opacity = 0.01;
			containerGrid.Children.Add(coverGrid);
			containerGrid.Children.Add(this.Mask);

			this.Child = containerGrid;

			this.IsOpen = true;

			this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(Cover_MouseLeftButtonDown);
			this.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(Cover_MouseRightButtonDown);
		}

		public event System.EventHandler<System.Windows.Input.MouseButtonEventArgs> Click;
		protected virtual void OnClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			System.Windows.Point position = e.GetPosition(this);
			if (this.Click != null)
				this.Click(sender, e);
		}

		void Cover_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			this.OnClick(sender, e);
		}

		void Cover_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			this.OnClick(sender, e);
		}

		void Window_StateChanged(object sender, EventArgs e)
		{
			if (((System.Windows.Window)sender).WindowState == System.Windows.WindowState.Minimized)
				this.IsOpen = false;
			else this.IsOpen = true;
		}

		void Window_Activated(object sender, EventArgs e)
		{
			this.IsOpen = true;
		}

		void Window_Deactivated(object sender, EventArgs e)
		{
			this.IsOpen = false;
		}

		void Window_LocationChanged(object sender, EventArgs e)
		{
			System.Windows.Point position = this.PlacementTarget.PointToScreen(new System.Windows.Point());
			if (position.X < 0)
				this.Width = this.PlacementTargetElement.ActualWidth/* - 20*/ + position.X;
			if (position.Y < 0)
				this.Height = this.PlacementTargetElement.ActualHeight/* - 20*/ + position.Y;
			if (position.X + this.PlacementTargetElement.ActualWidth > System.Windows.SystemParameters.VirtualScreenWidth)
				this.Width = this.PlacementTargetElement.ActualWidth - (position.X + this.PlacementTargetElement.ActualWidth - System.Windows.SystemParameters.VirtualScreenWidth);
			if (position.Y + this.PlacementTargetElement.ActualHeight > System.Windows.SystemParameters.VirtualScreenHeight)
				this.Height = this.PlacementTargetElement.ActualHeight - (position.Y + this.PlacementTargetElement.ActualHeight - System.Windows.SystemParameters.VirtualScreenHeight);

			//System.Windows.Window window = (System.Windows.Window)sender;
			//if (window.Left + window.ActualWidth > System.Windows.SystemParameters.VirtualScreenWidth)
			//    this.Width = this.PlacementTargetElement.ActualWidth - window.Left + window.ActualWidth - System.Windows.SystemParameters.VirtualScreenWidth;
			//if (window.Top + window.ActualHeight > System.Windows.SystemParameters.VirtualScreenHeight)
			//    this.Height = this.PlacementTargetElement.ActualHeight - window.Top + window.ActualHeight - System.Windows.SystemParameters.VirtualScreenHeight;
			
			this.HorizontalOffset = 1;
			this.HorizontalOffset = 0;
			this.VerticalOffset = 1;
			this.VerticalOffset = 0;
		}

		void PlacementTarget_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
		{
			//if (this.PlacementTargetElement.ActualWidth > 20)
				this.Width = this.PlacementTargetElement.ActualWidth/* - 20*/;
			//else this.Width = 0;

			//if (this.PlacementTargetElement.ActualHeight > 20)
				this.Height = this.PlacementTargetElement.ActualHeight/* - 20*/;
			//else this.Height = 0;
		}
	}
}
